package cn.lan.scau_hotel_service.controller;

import cn.lan.scau_hotel_service.entity.Room;
import cn.lan.scau_hotel_service.entity.RoomType;
import cn.lan.scau_hotel_service.entity.User;
import cn.lan.scau_hotel_service.service.RoomService;
import cn.lan.scau_hotel_service.service.RoomTypeService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(value = "/room")
public class RoomController {

    @Autowired
    RoomService roomService;
    @Autowired
    RoomTypeService roomTypeService;


    @ApiOperation(value = "添加和更新room",notes = "参数 room 对象")
    @RequestMapping(value="/{roomType_id}", method= RequestMethod.POST)
    public boolean addAndUpdateRoomType(@RequestBody Room room,@PathVariable int roomType_id) {
//        RoomType roomType = roomService.getRoomById(room.getId()).getRoomType();
        System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++"+ room.getCanRent());

        Room myroom = roomService.getRoomById(room.getId());

        if(myroom == null){
            myroom = new Room();
            myroom.setId(room.getId());
            myroom.setLocation(room.getLocation());
        }
        RoomType roomType = roomTypeService.getRoomTypeById(roomType_id);
        myroom.setRoomType(roomType);
        myroom.setCanRent(room.getCanRent());
        myroom.setRent(room.getRent());
        return roomService.saveRoom(myroom);
    }

    @ApiOperation(value = "删除 room",notes = "参数 room 的id")
    @RequestMapping(value="/{id}", method= RequestMethod.DELETE)
    public boolean deleteRoomType(@PathVariable int id) {
        return roomService.deleteRoomById(id);
    }

    @ApiOperation(value = "获取某个房间类型的剩余所有房间",notes = "参数 roomType 对象")
    @RequestMapping(value = "/list",method= RequestMethod.POST)
    public List<Room> getRooms(@RequestBody RoomType roomType){
        return roomService.getRoomsByRoomTypeAndCanRentTrueAndIsRentFalse(roomType);
    }

    @RequestMapping(value = "/all",method = RequestMethod.POST)
    public List<Room> getAllRooms(){
        List<Room> rooms = roomService.findAll();
        List<Room> roomList =new ArrayList<>();
        for(int i=0;i<rooms.size();i++) {
            roomList.add(roomService.getRoomById(rooms.get(i).getId()));
            System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" + roomList.get(i).getRoomType().getTypeName());
        }
        return roomList;
    }

    @RequestMapping(value = "/allRoomtype" , method = RequestMethod.POST)
    public List<RoomType> getAllRoomType(){
        List<Room> rooms = roomService.findAll();
        List<RoomType> roomTypes = new ArrayList<>();
        for(int i=0;i<rooms.size();i++){
            roomTypes.add(rooms.get(i).getRoomType());
        }
        return roomTypes;
    }

    @ApiOperation(value = "获取某个房间类型的剩余所有数",notes = "参数 roomType 对象")
    @RequestMapping(value = "/rentNumber",method= RequestMethod.POST)
    public int getRoomsCanRent(@RequestBody RoomType roomType){
        System.out.println("--------------" + roomType.getId());
        return roomService.countByRoomTypeCanRentTrueAndIsRentFalse(roomType);
    }


}
